<?php
    $PathToRoot = '../';
    require_once $PathToRoot . 'connect.php';

    $DB_ERROR = -1;

    function getReportedPost($connect)
    {
        global $ReportedPost;
        
        $sql = "SELECT reported_id FROM $ReportedPost WHERE reported_at >= NOW() - INTERVAL 1 DAY GROUP BY reported_id";
        
        $result = mysql_query ($sql, $connect);
        if ($result == false)
            return false;
        
        $list = array();
        while (false !== ($row = mysql_fetch_array($result)))
            $list[] = $row["reported_id"]; 
        
        return $list;
    }

    function sendEmail($to, $message)
    {
        $subject = "F-O Alert";
        $message = "<html><body>$message</body></html>";
        
        $headers  = "MIME-Version: 1.0\r\n";
        $headers .= "Content-type: text/html; charset=utf-8\r\n"; 
        $headers .= "From: Footbal-Online <noreply@football-online.com>\r\n";
            
        return mail($to, $subject, $message, $headers);
    }

    // script
    if (!isset($_POST["email"]) || empty($_POST["email"])) {
        error_log("Email address is not specified");
        exit;
    }
    
    $connect = mysql_connect($hostname, $username, $password);
    if (!$connect) {
        error_log("No DB connection!");
        exit;
    }
    mysql_select_db ($dbase, $connect);
    mysql_query ('SET CHARACTER SET cp1251', $connect);


    $reported = getReportedPost($connect);
    if ($reported === false) {
        error_log("Query error!");
        exit;
    }
    
    if (count($reported) > 0) {
        $message = "Появились новые жалобы (" . count($reported) . ") :";
        foreach ($reported as $r)
            $message .= "<br>№" . $r;
        $message .= "<br><br><a href=\"" . "http://f-o.com.ua/admin/editgb.php\">" . "Перейти на страницу администрирования</a>" ;
 
        if (!sendEmail($_POST["email"], $message))
            error_log("Error sending email!");
    }
?>